/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import crm.salesOrderProcessing.entity.ProductEntity;
import crm.salesOrderProcessing.session.ProductManageSessionBean;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class ManagePromotionManagedBean {
    @EJB
    private ProductManageSessionBean productSession;
    private List<ProductEntity> productList;
    private boolean canEdit = false;
    
    @EJB
    EmployeeSessionBean employee;

    /** Creates a new instance of ManagePromotionManagedBean */
    public ManagePromotionManagedBean() {
        productSession = new ProductManageSessionBean();
    }
    
    public void initView(PhaseEvent event) {
        if (productList == null){
            setProductList(productSession.getAllProducts());
        }
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        SalesmanEntity user = employee.getSalesmanById(loginId);
        if (user.getSalesManLevel().equals("4")){
            canEdit = true;
        }
        else{
            canEdit = false;
        }
        
    }
    
    
    public void save(ActionEvent action){
        for (int i=0;i<getProductList().size();i++){
            productSession.saveChanges(getProductList().get(i));
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Promotional Price Set Successfully", ""));
    }

    /**
     * @return the productList
     */
    public List<ProductEntity> getProductList() {
        return productList;
    }

    /**
     * @param productList the productList to set
     */
    public void setProductList(List<ProductEntity> productList) {
        this.productList = productList;
    }

    /**
     * @return the canEdit
     */
    public boolean isCanEdit() {
        return canEdit;
    }

    /**
     * @param canEdit the canEdit to set
     */
    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }

}
